package com.wangshidai.t2;

import java.io.*;
import java.net.Socket;

public class ClientDemo {

    public static void main(String[] args)
    {
        try {
            Socket socket=new Socket("127.0.0.1",8888);
            System.out.println("服务器连接成功");

            //发送消息就是流的操作
            HttpMessageUtil.sendMessage(socket,"慢走不送！！！");
            OutputStream os = socket.getOutputStream();
            PrintStream ps=new PrintStream(os,true);
            ps.println("哥，你吃饭了吗?");  //发送消息

            //接收客户端的消息
            InputStream inputStream = socket.getInputStream();
            InputStreamReader isr=new InputStreamReader(inputStream);
            BufferedReader br=new BufferedReader(isr);

            String message = br.readLine();//读取消息
            System.out.println("客户端说:"+message);

            ps.close();
            br.close();

        } catch (IOException e) {
            System.out.println("服务器连接失败");
            e.printStackTrace();
        }
    }

}
